package CharacterDevelop;

import java.util.Scanner;

/**
 * ClassName:StringTest009
 * Package:CharacterDevelop
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/1/16-13:12
 * @Version:v1.0
 */
public class StringTest009 {
    /*金额转换*/
    //2135 --->  零佰零拾零万贰仟壹佰叁拾伍元

    public static void main(String[] args) {
        //1.键盘录入金额
        int money;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请录入一个金额:");
            money = sc.nextInt();
            if (money >= 0 && money <= 9999999) {
                break;
            } else {
                System.out.println("金额无效");
            }
        }
        //定义一个变量用来表示钱的大写
        String moneyStr = "";
        //2.得到money里面的每一个数字
        while (true) {
            //从右往左获取数字,因为右侧是数据的个位
            int ge = money % 10;
            String capitalNumber = getCapitalNumber(ge);
            moneyStr = capitalNumber + moneyStr;

            money = money / 10;
            //如果数字上的每一位全部获取到了,那么money记录的就是0,此时循环结束
            if (money == 0) {
                break;
            }
        }

        //3.在前面补0,补齐7位
        int count = 7 - moneyStr.length();
        for (int i = 0; i < count; i++) {
            moneyStr = "零" + moneyStr;
        }


        //4.插入单位
        //定义一个数组表示单位
        String result="";
        String[]arr={"佰","拾","万","仟","佰","拾","元"};
        //遍历moneyStr,然后把arr的单位插进去
        for (int i = 0; i < moneyStr.length(); i++) {
            char c = moneyStr.charAt(i);
            //把大写数字和单位拼接到result中
            result = result+c+arr[i];
        }
        System.out.println(result);
    }

    //定义一个方法把数字变成大写的中文
    public static String getCapitalNumber(int number) {
        //定义数组,让数字跟大写的中文产生一个对应关系
        String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        return arr[number];
    }
}
